package 优先算法.双指针.复写零;

import java.util.Arrays;

/**
 *网址:https://leetcode.cn/problems/duplicate-zeros/
 */
public class 复写零 {
    public static void main(String[] args) {
        Solution so = new Solution();
        int[] arr = {1,0,2,3,0,4};
        so.duplicateZeros(arr);
        System.out.println(Arrays.toString(arr));
    }
}
class Solution {
    public void duplicateZeros(int[] arr) {
        int dest=-1,cur=0;
        //找到从后面开始遍历的index
        while(dest<arr.length-1){
            if(arr[cur]!=0){
                dest++;
            }else{
                dest+=2;
            }

            if(dest<arr.length-1){
                cur++;
            }
        }

        //处理边界
        if(dest==arr.length){
            arr[dest-1]=0;
            cur--;
            dest-=2;
        }

        //位置找到了 从后遍历
        while(cur>=0){
            if(arr[cur]!=0){
                arr[dest--]=arr[cur--];
            }else{
                arr[dest--]=0;
                arr[dest--]=0;
                cur--;
            }
        }
    }
}